#pragma once
#include<iostream>
#include<string>
#include"Protocol.hpp"

using namespace std;

class ServerCal
{
public:
    ServerCal()
    {}
    Response core_Calculator(Request req)
    {
        Response res;
        switch(req._opr)
        {
            case '+':
                res._result=req._x+req._y,res._code=0;
                break;
            case '-':
                res._result=req._x-req._y,res._code=0;
                break;
            case '*':
                res._result=req._x*req._y,res._code=0;
                break;
            case '/':
                if(req._y==0)res._result=0,res._code=1;
                else res._result=req._x/req._y,res._code=0;
                break;
            case '%':
                if(req._y==0)res._result=0,res._code=2;
                else res._result=req._x%req._y,res._code=0;
                break;
            default:
                res._code=2;
                break;
        }
        return res;
    }
    string Calculator(string& package)
    {
        //去封装、解包package
        string content;//接收有效载荷
        if(Decode(package,content)==0)return "";
        Request req;//将有效载荷赋给req的成员变量
        if(req.Deserialize(content)==0)return "";

        //计算
        Response res=core_Calculator(req);
        string str;
        res.Serialize(str);

        str=Encode(str);//添加报头，即封装计算结果，准备向网络发送
        return str;
    }

    ~ServerCal()
    {}
};